package com.wicket.okrcomponent.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrcomponent.base.service.MOmsMessageTemplateService;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.common.tools.StringUtils;
import com.wicket.okrcomponent.common.util.CommonFunctionHelper;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageTemplateMapper;
import com.wicket.okrcomponent.dal.po.mbg.OmsMessage;
import com.wicket.okrcomponent.dal.po.mbg.OmsMessageTemplate;
import com.wicket.okrcomponent.base.service.dto.req.*;
//import com.wicket.okrcomponent.base.service.dto.res.*;

import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsMessageTemplateServiceImpl
  extends ServiceImpl<OmsMessageTemplateMapper, OmsMessageTemplate>
  implements MOmsMessageTemplateService {

  @Autowired
  OmsMessageTemplateMapper omsMessageTemplateMapper;
/**
   * 2-5-01查询消息模板详情[3991]
   * gen by moon at 9/14/2022, 4:09:16 PM
   */
 @Trace(operationName = "2-5-01查询消息模板详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  OmsMessageTemplate  queryMessageTemplateDetail(QueryMessageTemplateDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<OmsMessageTemplate> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageTemplate> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                OmsMessageTemplate retData = retList.get(0);

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }
/**
   * 2-5-01修改消息模板[6767]
   * gen by moon at 5/16/2023, 6:36:37 PM
   */
 @Trace(operationName = "2-5-01修改消息模板")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateMessageTemplate(OmsMessageTemplate omsMessageTemplate) {

        return super.updateById(omsMessageTemplate);
  }
/**
   * 2-5-01删除消息模板[6770]
   * gen by moon at 5/16/2023, 6:36:37 PM
   */
 @Trace(operationName = "2-5-01删除消息模板")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteMessageTemplate(String messageTemplateId ) {

      return super.removeById(messageTemplateId);
  }
/**
   * 2-5-01新增消息模板[6715]
   * gen by moon at 5/16/2023, 8:50:54 PM
   */
 @Trace(operationName = "2-5-01新增消息模板")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addMessageTemplate(OmsMessageTemplate omsMessageTemplate) {
     omsMessageTemplate.setMessageTemplateId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
omsMessageTemplate.setIsArchive("FALSE");
omsMessageTemplate.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsMessageTemplate.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(omsMessageTemplate!=null && omsMessageTemplate.getSubjectLifeCycle()!=null &&  omsMessageTemplate.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageTemplate.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
omsMessageTemplate.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsMessageTemplate.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(omsMessageTemplate!=null && omsMessageTemplate.getSubjectLifeCycle()!=null && omsMessageTemplate.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             omsMessageTemplate.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
omsMessageTemplate.setIsValid("FALSE");
omsMessageTemplate.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
omsMessageTemplate.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(omsMessageTemplate)){
        return omsMessageTemplate.getMessageTemplateId();
     }else {
        return "-1";
     }

  }
/**
   * 2-5-01查询消息模板列表[6717]
   * gen by moon at 5/16/2023, 8:50:57 PM
   */
 @Trace(operationName = "2-5-01查询消息模板列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsMessageTemplate>  queryMessageTemplateList(QueryMessageTemplateListReq reqDto) {

        try {
            QueryWrapper<OmsMessageTemplate> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageTemplate.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageTemplate.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageTemplate.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageTemplate>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

}

